{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}R:\WSV2\TBu_AKe\Spatial_NEW\spatial_3_wmat_grid.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}29 Mar 2023, 14:34:09
{txt}
{com}. 
. 
. cd "R:\WSV2\TBu_AKe\Spatial_NEW"
{res}R:\WSV2\TBu_AKe\Spatial_NEW
{txt}
{com}. 
. 
. use "C:\Users\hy65byfe\Desktop\smerge_0712\spatial_2_collapse_s.dta" // data set with 4950 cells, created in spatial_2_*  
{txt}
{com}. 
. 
. scalar p = 0 // if 0, straight distance without decay : x^0 = 1, so all are equal.
{txt}
{com}. scalar sigma = 47*0.59 // sigma at comp. line 
{txt}
{com}. scalar kappa = 51.7*0.59 // kappa at comp. line 
{txt}
{com}. scalar D = 3 // width of increment 
{txt}
{com}. 
. ***************************
. **** reduce dimensions ****
. ***************************
. 
. keep if year == 2011 & ccode == 1 // reduce data in memory 
{txt}(549,450 observations deleted)

{com}.   
. sort cell // now sorted: first bunching (1-50), then restricted (51-1015) then rest. 
{txt}
{com}. 
. gsort -bunching +cell 
{txt}
{com}. gen bcell = _n if bunching == 1 // identify B
{txt}(4,900 missing values generated)

{com}. 
. gsort -restricted +cell 
{txt}
{com}. gen rcell = _n if restricted == 1 // identify R
{txt}(2,100 missing values generated)

{com}. 
. 
. **********************************************
. *** define upper and lower bounds of cells ***
. **********************************************
.                 
.         ** gen mid-point for E 
. *gen a_mid = a_bin 
. *gen e_mid = `=kappa' + `=sigma'*a_mid //  
. 
.         ** gen upper and lower level of E at mid-point (e.g. 6kg, 6.5kg ...)
. replace e_bin = k_bin + a_bin*`=sigma' // gravity center: upper edge of mid-point 
{txt}(4,950 real changes made)

{com}. gen e_ml = k_bin + a_bin*`=sigma' - `=D' // lower edge of mid-point 
{txt}
{com}. 
. 
. replace eei_bin = (e_bin/(51.7+47*a_bin))* 100 // assert that bins are consistent with eei boundaries. 
{txt}(4,944 real changes made)

{com}. 
.         ** label as safety net 
. label variable e_bin "E_i at midpoint"
{txt}
{com}. 
. label variable a_min "a_i left"
{txt}
{com}. label variable a_max "a_i right"
{txt}
{com}. label variable k_bin "kappa (std. E)"
{txt}
{com}. 
. 
. keep eei_bin a_* e_* k_bin s_bin eei_bin cell bcell rcell // cut 
{txt}
{com}. 
. sort cell // order is crucial 
{txt}
{com}. xtset cell 

{txt}{col 1}Panel variable: {res}cell{txt} (balanced)

{com}. spset cell 
{res}
{txt}{ralign 16:Sp dataset}: {res:spatial_2_collapse_s.dta}
Linked shapefile: <none>
{ralign 16:Data}: Cross sectional
{ralign 16:Spatial-unit ID}: {res:_ID} (equal to {res:cell})
{ralign 16:Coordinates}: <none>

{com}. 
. ***********************************
. ******* start loops ***************
. ***********************************
. 
. ************************************     
. *  3a: neighborhood distance count *
. ************************************
. 
. gen k_run = (s_bin/3) // running index with 0,1,2,3,4,5,6 ... for cells in E-direction 
{txt}
{com}. 
. gen a_run = (a_bin*2 - 6) +1 // running index with 1,2,3, ... for cells in a-direction (must start at 1)
{txt}
{com}. 
. * scalar list 
. 
. **************************************
. * 3b: define scalar for every cell ***
. **************************************
. 
. 
. sum cell, meanonly 
{txt}
{com}. scalar cmax=`r(max)'
{txt}
{com}. local c = `r(max)' // scalar for each cell.  
{txt}
{com}.         
. sum bcell, meanonly 
{txt}
{com}. scalar bmax=`r(max)'
{txt}
{com}. 
. sum rcell, meanonly 
{txt}
{com}. scalar rmax=`r(max)'
{txt}
{com}. 
. cd "$store"
{res}R:\WSV2\TBu_AKe\Spatial_NEW\Data
{txt}
{com}. 
. forvalues q = 5(5)40 {c -(}
{txt}  2{com}.         preserve 
{txt}  3{com}. 
.         scalar q = `q' // after q cells, jump to next strip 
{txt}  4{com}. 
.         sum k_run, meanonly
{txt}  5{com}.         scalar max_k = `r(max)'+`=q' // 
{txt}  6{com}.         scalar min_k = `r(min)'
{txt}  7{com}. 
.         sum a_run, meanonly
{txt}  8{com}.         scalar max_a = `r(max)' +`=q' // going left after 1to1 in grid 
{txt}  9{com}.         scalar min_a = `r(min)'
{txt} 10{com}. 
.         *** create group address *** 
. 
.         egen k_run2 =cut(k_run),at(0(`=q')`=max_k') // first 5 addresses get 1, second 5 get 2: like city blocks. 
{txt} 11{com}. 
.         egen k_group = group(k_run2) 
{txt} 12{com}.         replace k_group = k_group - 1 // start at zero 
{txt} 13{com}. 
.         *sum k_run if missing(k_group) // all negative. 
. 
. 
. **************************************
. * 3b: define scalars for every cell ***
. *************************************
. 
.         forvalues i = 1/`=cmax' {c -(}
{txt} 14{com}.         
.                 * a-address  
.                 quietly sum a_run if cell == `i'
{txt} 15{com}.                 local a = r(mean)
{txt} 16{com}.                 scalar a`i' = `a'
{txt} 17{com}.         
.                 * std.  
.                 quietly sum k_group if cell == `i'
{txt} 18{com}.                 local k = r(mean)
{txt} 19{com}.                 scalar k`i' = `k'
{txt} 20{com}.         
.                 * eei 
.                 quietly sum eei_bin if cell == `i'
{txt} 21{com}.                 local x = r(mean)
{txt} 22{com}.                 scalar x`i' = `x'
{txt} 23{com}. 
.         {c )-}
{txt} 24{com}. 
.         
.         *************************
.         * 4a: define nieghbors ***
.         *************************
. 
.         forvalues m = 1/`=bmax' {c -(}
{txt} 25{com}.      quietly sum cell if bcell==`m' //  
{txt} 26{com}.      local i = r(mean)
{txt} 27{com}.           * i contains cellid of cell i in B     
.           forvalues l = 1/`=cmax' {c -(}
{txt} 28{com}.           quietly sum cell if cell==`l'
{txt} 29{com}.           local j = r(mean)
{txt} 30{com}.                   * j contains cellid of cell j in R 
.                   
.                   * knock-out condition 
.                   scalar x_`i'_`j'      = cond( x`j' > x`i'   &  inrange(x`j', 59, 87)          ,       1,0) // only neighbors in B. 
{txt} 31{com}.                  
.                   * define neighbors 
.                   scalar no_`i'_`j' =  cond( -1*(a`i' - a`j')   == k`j',       1,0) // nord-ost : i < j 
{txt} 32{com}.                   scalar nw_`i'_`j' =  cond(    (a`i' - a`j')   == k`j',       1,0) // nord-west: i > j  
{txt} 33{com}.                   
.                   {c )-}
{txt} 34{com}.         
.      {c )-}   
{txt} 35{com}. 
. 
.         *********************************
.         *** 4b: transform to matrix *****
.         *********************************
. 
.         *** neighborhood matrices: MANUAL: Sektors 
.         mat define X  = I(`=cmax') // knock out 
{txt} 36{com}.         mat define O`q' = I(`=cmax') // east  
{txt} 37{com}.         mat define W`q' = I(`=cmax') // sw 2
{txt} 38{com}. 
. 
.         *********************
.         **  5: fill matrix **
.         *********************
. 
. 
.         forvalues m = 1/`=bmax' {c -(}
{txt} 39{com}.                 quietly sum cell if bcell==`m' & bcell != . 
{txt} 40{com}.                 local i = r(mean)
{txt} 41{com}.                 forvalues l = 1/`=cmax' {c -(}
{txt} 42{com}.                         quietly sum cell if cell==`l' 
{txt} 43{com}.                         local j = r(mean)
{txt} 44{com}.                   ** run through combinations and put in distance **
.                         mat X[`m',`l']=(x_`i'_`j')
{txt} 45{com}.                         mat O`q'[`m',`l']=(no_`i'_`j')
{txt} 46{com}.                         mat W`q'[`m',`l']=(nw_`i'_`j')
{txt} 47{com}.          {c )-}
{txt} 48{com}.                 forvalues c = 1/`=cmax' {c -(}
{txt} 49{com}.                 ** replace own-cross with zero (identical cell)
.                 mat X[`c',`c']=0
{txt} 50{com}.                 mat O`q'[`c',`c']=0
{txt} 51{com}.                 mat W`q'[`c',`c']=0
{txt} 52{com}.                         {c )-}
{txt} 53{com}.     {c )-} 
{txt} 54{com}.         
.         matrix XO`q'=hadamard(X, O`q')
{txt} 55{com}.         matrix XW`q'=hadamard(X, W`q')
{txt} 56{com}. 
. 
.         svmat XO`q', names(o`q'_)
{txt} 57{com}.         svmat XW`q', names(w`q'_)
{txt} 58{com}. 
.         spmatrix fromdata O`q' = o`q'_1 - o`q'_4950, normalize(none)
{txt} 59{com}.         spmatrix save O`q' using O`q'.stswm, replace
{txt} 60{com}. 
.         spmatrix fromdata W`q' = w`q'_1 - w`q'_4950, normalize(none)
{txt} 61{com}.         spmatrix save W`q' using W`q'.stswm, replace
{txt} 62{com}. 
.         save spatial_3_wmat_grid_`q', replace
{txt} 63{com}. 
.         drop k_run2
{txt} 64{com}.         drop k_group 
{txt} 65{com}.         
.         restore 
{txt} 66{com}. 
. {c )-}
{txt}(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O5} saved in file {bf:O5.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W5} saved in file {bf:W5.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_5.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_5.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O10} saved in file {bf:O10.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W10} saved in file {bf:W10.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_10.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_10.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O15} saved in file {bf:O15.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W15} saved in file {bf:W15.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_15.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_15.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O20} saved in file {bf:O20.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W20} saved in file {bf:W20.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_20.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_20.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O25} saved in file {bf:O25.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W25} saved in file {bf:W25.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_25.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_25.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O30} saved in file {bf:O30.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W30} saved in file {bf:W30.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_30.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_30.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O35} saved in file {bf:O35.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W35} saved in file {bf:W35.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_35.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_35.dta{rm}
saved
{p_end}
(2,100 missing values generated)
(2,100 missing values generated)
(2,850 real changes made)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:O40} saved in file {bf:O40.stswm}){p_end}
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}
{res}{p 2 2 2}{txt}(matrix {bf:W40} saved in file {bf:W40.stswm}){p_end}
{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_40.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_40.dta{rm}
saved
{p_end}

{com}. 
. 
. ********************** repeat for horizontal and vertical matrix *******************************
. 
.         
.         scalar q = 0 // no jumps  
{txt}
{com}.         local q = 0 //  local macro 
{txt}
{com}. 
.         sum k_run, meanonly
{txt}
{com}.         scalar max_k = `r(max)'+`=q' // 
{txt}
{com}.         scalar min_k = `r(min)'
{txt}
{com}. 
.         sum a_run, meanonly
{txt}
{com}.         scalar max_a = `r(max)' +`=q' // irrelevant for this case, q=0. 
{txt}
{com}.         scalar min_a = `r(min)'
{txt}
{com}. 
.         
.         **************************************
.         * 3b: define scalars for every cell ***
.         *************************************
. 
. 
.         forvalues i = 1/`=cmax' {c -(}
{txt}  2{com}.         
.                 * a-address  
.                 quietly sum a_run if cell == `i'
{txt}  3{com}.                 local a = r(mean)
{txt}  4{com}.                 scalar a`i' = `a'
{txt}  5{com}.                 
.                 /* 
>                 * std.  
>                 quietly sum k_group if cell == `i'
>                 local k = r(mean)
>                 scalar k`i' = `k'
>                 */ 
.                 
.                 * eei 
.                 quietly sum eei_bin if cell == `i'
{txt}  6{com}.                 local x = r(mean)
{txt}  7{com}.                 scalar x`i' = `x'
{txt}  8{com}.                 
.                 * e_bin (low-left corner)
.                 quietly sum e_bin if cell == `i'
{txt}  9{com}.                 local e = r(mean)
{txt} 10{com}.                 scalar e`i' = `e'
{txt} 11{com}. 
.         {c )-}
{txt}
{com}. 
. 
.         forvalues m = 1/`=bmax' {c -(}
{txt}  2{com}.      quietly sum cell if bcell==`m' // 
{txt}  3{com}.      local i = r(mean)
{txt}  4{com}.           * i contains cellid of cell i in B     
.           forvalues l = 1/`=cmax' {c -(}
{txt}  5{com}.           quietly sum cell if cell==`l'
{txt}  6{com}.           local j = r(mean)
{txt}  7{com}.                   * j contains cellid of cell j in R 
.                   
.                   * knock-out condition  
.                   scalar x_`i'_`j'      = cond( x`j' > x`i'   &  inrange(x`j', 59, 87)          ,       1,0) // only neighbors in B. 
{txt}  8{com}.                  
.                   
.                   * define neighbors 
.                   scalar r_`i'_`j' =  cond(a`i' ==  a`j',                              1,0) // rook 
{txt}  9{com}.                   scalar h_`i'_`j' =  cond(abs(e`i'-e`j') <= 6,       1,0) //    
{txt} 10{com}.                   {c )-}
{txt} 11{com}.         
.      {c )-}   
{txt}
{com}. 
. 
.         *********************************
.         *** 4b: transform to matrix *****
.         *********************************
. 
.         *** neighborhood matrices: 
.         mat define X  = I(`=cmax') // knock out 
{txt}
{com}.         mat define R = I(`=cmax') //  vertical / rook 
{txt}
{com}.         mat define H = I(`=cmax') // horizontal
{txt}
{com}. 
. 
.         ************************
.         **  5: fill matrix *****
.         ************************
. 
.         forvalues m = 1/`=bmax' {c -(}
{txt}  2{com}.                 quietly sum cell if bcell==`m' & bcell != . 
{txt}  3{com}.                 local i = r(mean)
{txt}  4{com}.                 forvalues l = 1/`=cmax' {c -(}
{txt}  5{com}.                         quietly sum cell if cell==`l' 
{txt}  6{com}.                         local j = r(mean)
{txt}  7{com}.                   ** run through combinations and put in distance **
.                         mat X[`m',`l']=(x_`i'_`j')
{txt}  8{com}.                         mat R[`m',`l']=(r_`i'_`j')
{txt}  9{com}.                         mat H[`m',`l']=(h_`i'_`j')
{txt} 10{com}.          {c )-}
{txt} 11{com}.                 forvalues c = 1/`=cmax' {c -(}
{txt} 12{com}.                 ** replace own-cross with zero (identical cell)
.                 mat X[`c',`c']=0
{txt} 13{com}.                 mat R[`c',`c']=0
{txt} 14{com}.                 mat H[`c',`c']=0
{txt} 15{com}.                         {c )-}
{txt} 16{com}.     {c )-} 
{txt}
{com}.         
.         matrix XR=hadamard(X, R)
{txt}
{com}.         matrix XH=hadamard(X, H)
{txt}
{com}. 
. 
.         svmat XR, names(r_)
{txt}
{com}.         svmat XH, names(h_)
{txt}
{com}. 
.         spmatrix fromdata R = r_1 - r_4950, normalize(none)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}

{com}.         spmatrix save R using R.stswm, replace
{res}{p 2 2 2}{txt}(matrix {bf:R} saved in file {bf:R.stswm}){p_end}

{com}. 
.         spmatrix fromdata H = h_1 - h_4950, normalize(none)
{res}{p 2 2 2}{txt}(weighting matrix contains 4900 islands){p_end}

{com}.         spmatrix save H using H.stswm, replace
{res}{p 2 2 2}{txt}(matrix {bf:H} saved in file {bf:H.stswm}){p_end}

{com}. 
.         save spatial_3_wmat_grid_polar, replace
{txt}{p 0 4 2}
(file {bf}
spatial_3_wmat_grid_polar.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
spatial_3_wmat_grid_polar.dta{rm}
saved
{p_end}

{com}. 
.         
. log close 
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}R:\WSV2\TBu_AKe\Spatial_NEW\spatial_3_wmat_grid.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}29 Mar 2023, 15:41:13
{txt}{.-}
{smcl}
{txt}{sf}{ul off}